package handler

import (
	"order-mgmt/models"
	"strconv"

	"github.com/gin-gonic/gin"
)

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var goods []models.Good
	models.DB.Offset(offset).Limit(size).Find(&goods)
	var total int64
	models.DB.Model(&models.Good{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  goods,
		"total": total,
	})
	return
}
func Add(c *gin.Context) {
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))
	good_no := c.PostForm("good_no")
	if good_name == "" || good_no == "" || good_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"mgs":  "字段不可为空",
		})
	}

	data := models.Good{
		GoodNae:   good_name,
		GoodNo:    good_no,
		GoodPrice: good_price,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"mgs":  "",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"mgs":  "添加成功",
		})
		return
	}

}
